package 阻塞队列;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BlockingQueueTest {
    public static void execute(Queue<Integer> queue) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for(int i = 1; i <= 1000; i++){
            final int finalNum = i;
            executorService.execute(() -> {
                try{
                    queue.put(finalNum);
                    Integer take = queue.take();
                    System.out.println("item: " + take);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        executorService.shutdown();
    }
    public static void main(String[] args) {
        Queue<Integer> q1 = new BlockingQueueWithSync<>(5);
        execute(q1);
        Queue<Integer> q2 = new BlockingQueueWithLock<>(5);
        execute(q2);
    }
}
